package com.devops.admin.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.devops.admin.po.DOSK8sServiceMonitor;
import com.devops.admin.service.DOSK8sServiceMonitorService;
import com.devops.admin.util.K8sClientUtil;
import com.devops.admin.util.TimeUtil;
import com.devops.admin.vo.DOSNamespaceChart;
import com.devops.constant.DevOpsConstant;
import io.fabric8.kubernetes.api.model.*;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.Gettable;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@org.springframework.stereotype.Service
public class DOSK8sServiceMonitorServiceImpl implements DOSK8sServiceMonitorService {

    static final String KIND = "ServiceMonitor";
    static final String APIVERSION= "monitoring.coreos.com/v1";

    @Override
    public List<DOSK8sServiceMonitor> getK8sServiceMonitorInfo(DOSNamespaceChart namespaceChart) {
        if (namespaceChart.getClient() == null) {
            KubernetesClient client = K8sClientUtil.getKubernetesClient(namespaceChart.getConfigPath(),namespaceChart.getConfigContent());
            namespaceChart.setClient(client);
        }
        NonNamespaceOperation<GenericKubernetesResource, GenericKubernetesResourceList, Resource<GenericKubernetesResource>> operation =
                namespaceChart.getClient().genericKubernetesResources(APIVERSION, KIND).inNamespace(namespaceChart.getNamespace());

        List<GenericKubernetesResource> serviceMonitors = operation.resources().map(Gettable::get).collect(Collectors.toList());
        String releaseName = namespaceChart.getReleaseName();
        List<DOSK8sServiceMonitor> dosk8sServiceMonitors = new ArrayList<>();

        for (GenericKubernetesResource serviceMonitor : serviceMonitors) {
            ObjectMeta metadata = serviceMonitor.getMetadata();
            LinkedHashMap<String, Object> spec = serviceMonitor.get("spec");
            JSONObject specJson = JSONUtil.parseObj(spec);
            String appName = specJson.getJSONObject("selector").getJSONObject("matchLabels").getStr("app");

            if (releaseName.equals(appName)) {
                DOSK8sServiceMonitor dosk8sServiceMonitor = DOSK8sServiceMonitor.builder()
                        .name(metadata.getName())
                        .namespace(metadata.getNamespace())
                        .created(TimeUtil.getDate(metadata.getCreationTimestamp()))
                        .labels(K8sClientUtil.dealMapToStringArray(metadata.getLabels()))
                        .annotations(K8sClientUtil.dealMapToStringArray(metadata.getAnnotations()))
                        .spec(specJson.toStringPretty())
                        .build();
                dosk8sServiceMonitors.add(dosk8sServiceMonitor);
            }
        }
        return dosk8sServiceMonitors;
    }
}
